package com.kidedu.action;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.kidedu.entity.KTeacher;
import com.kidedu.service.IKTeacherService;
import com.kidedu.util.SpringContextUtil;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class KTeacherAction {
   private int pageNo;
   private int pageSize;
   private String username;
   private String password;
   private String t_id;
   private String tinfo;
   public String getTinfo() {
	return tinfo;
}
public void setTinfo(String tinfo) {
	this.tinfo = tinfo;
}
private KTeacher teacher;
   private JSONObject json=new JSONObject();
   private 	 IKTeacherService  kTeacherService= (IKTeacherService) SpringContextUtil.getBean("iKTeacherService");
   
   public String teacherListFY()
   {
	   json=kTeacherService.TeacherInfoFY(pageSize, pageNo);
	   
	   return Action.SUCCESS;
	   
	   
   }
   public String Login()
   {
	    if(kTeacherService.Login(username, password)){
	    	 ActionContext.getContext().getSession().put("ctx", ServletActionContext.getServletContext().getContextPath());
	    	return Action.SUCCESS;
	    }
	    return "fail";
   }
   public String TeacherLoginOut()
   {
	   HttpServletRequest req = ServletActionContext.getRequest();
		req.getSession().removeAttribute("TEACHER");
	   return Action.SUCCESS;
   }
   public String updateTeacher()
   {
		  String[] teacherinfo=tinfo.split(",");
	     teacher=new KTeacher();
	     teacher.setTusername(teacherinfo[1]);
	     teacher.setTid(teacherinfo[0]);
	     teacher.setTlevel(Byte.valueOf(teacherinfo[2]));
	     teacher.setTtelnumber(teacherinfo[3]);
	   if(kTeacherService.updateTeacher(teacher))
		   json.put("message", "success");
	   else
		   json.put("message", "fail");
	        return Action.SUCCESS;   
   }
   public String deleteTeacher()
   {
	   if(kTeacherService.deleteTeacher(t_id))
		   json.put("message", "success");
	   else
		   json.put("message", "fail");
	   return Action.SUCCESS;
   }
   
   public String AddSingleTeacher()
   {
	   if(kTeacherService.addSingleTeacher(teacher))
		   json.put("message", "success");
	   else
		   json.put("message", "fail");
	   return Action.SUCCESS;
   }
public int getPageNo() {
	return pageNo;
}
public void setPageNo(int pageNo) {
	this.pageNo = pageNo;
}
public int getPageSize() {
	return pageSize;
}
public void setPageSize(int pageSize) {
	this.pageSize = pageSize;
}
public JSONObject getJson() {
	return json;
}
public void setJson(JSONObject json) {
	this.json = json;
}
public String getUsername() {
	return username;
}

public void setUsername(String username) {
	this.username = username;
}

public String getPassword() {
	return password;
}

public void setPassword(String password) {
	this.password = password;
}
public String getT_id() {
	return t_id;
}
public void setT_id(String t_id) {
	this.t_id = t_id;
}

}
